<template>
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="true" :title="title" :type="type" v-model="isVisible" :width="width" top="50px" append-to-body>
        <el-form ref="dataRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="字典类型">
                <el-input v-model="form.dictType" :disabled="true" />
            </el-form-item>
            <el-form-item label="数据标签" prop="dictLabel">
                <el-input v-model="form.dictLabel" placeholder="请输入数据标签" />
            </el-form-item>
            <el-form-item label="数据键值" prop="dictValue">
                <el-input v-model="form.dictValue" placeholder="请输入数据键值" />
            </el-form-item>
            <el-form-item label="样式属性" prop="cssClass">
                <el-input v-model="form.cssClass" placeholder="请输入样式属性" />
            </el-form-item>
            <el-form-item label="显示排序" prop="dictSort">
                <el-input-number v-model="form.dictSort" controls-position="right" :min="0" />
            </el-form-item>
            <el-form-item label="回显样式" prop="listClass">
                <el-select v-model="form.listClass">
                    <el-option v-for="item in listClassOptions" :key="item.value" :label="item.label + '(' + item.value + ')'" :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status">
                    <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="close">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script setup lang="ts" name="DictDataItemEdit">
    import { getCurrentInstance, ref, computed, watch } from "vue";
    import useDictStore from "@/store/modules/dict";
    import { addData, updateData } from "@/api/system/dict/data";
    const props = defineProps({
        dialogVisible: {
            type: Boolean,
            default: false,
        },
        type: {
            type: String,
            default: "add",
        },
        dictData: {
            type: Object,
            default: {},
        },
    });
    const emit = defineEmits(["close", "success"]);
    const { proxy } = getCurrentInstance() as any;
    const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
    // 数据标签回显样式
    const listClassOptions = ref([
        { value: "default", label: "默认" },
        { value: "primary", label: "主要" },
        { value: "success", label: "成功" },
        { value: "info", label: "信息" },
        { value: "warning", label: "警告" },
        { value: "danger", label: "危险" },
    ]);
    const rules = ref({
        dictLabel: [{ required: true, message: "数据标签不能为空", trigger: "blur" }],
        dictValue: [{ required: true, message: "数据键值不能为空", trigger: "blur" }],
        dictSort: [{ required: true, message: "数据顺序不能为空", trigger: "blur" }],
    });
    const screenWidth = ref(0);
    const width = ref(initWidth());
    const form = ref(props.dictData);
    watch(
        () => props.dictData,
        () => {
            form.value = props.dictData;
        },
        {
            deep: true,
        }
    );
    const isVisible = computed({
        get: () => props.dialogVisible,
        set: (val) => {
            close();
            reset();
        },
    });
    const title = computed(() => (props.type === "add" ? "新增" : "修改"));
    window.onresize = () => {
        return (() => {
            width.value = initWidth();
        })();
    };

    function initWidth() {
        screenWidth.value = document.body.clientWidth;
        if (screenWidth.value < 991) {
            return "90%";
        } else if (screenWidth.value < 1400) {
            return "45%";
        } else {
            return "800px";
        }
    }

    /** 取消按钮 */
    function close() {
        reset();
        emit("close");
    }
    /** 表单重置 */
    function reset() {
        form.value = {
            dictCode: undefined,
            dictLabel: undefined,
            dictValue: undefined,
            cssClass: undefined,
            listClass: "default",
            dictSort: 0,
            status: "0",
            remark: undefined,
        };
    }

    /** 提交按钮 */
    function submitForm() {
        proxy.$refs["dataRef"].validate((valid: any) => {
            if (valid) {
                if (form.value.dictCode != undefined) {
                    updateData(form.value).then((response) => {
                        useDictStore().removeDict(form.value.dictType);
                        proxy.$modal.msgSuccess("修改成功");
                        emit("success");
                    });
                } else {
                    addData(form.value).then((response) => {
                        useDictStore().removeDict(form.value.dictType);
                        proxy.$modal.msgSuccess("新增成功");
                        emit("success");
                    });
                }
            }
        });
    }
</script>
<style lang="scss" scoped></style>
